#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"

void conf_cpy(CONF *dst, CONF *src)
{
	memcpy(dst, src, sizeof(CONF));

	dst->url_server = (struct addr *)malloc(sizeof(struct addr) * src->url_server_num);
	memcpy(dst->url_server, src->url_server, sizeof(struct addr) * src->url_server_num);

	dst->page_server = (struct addr *)malloc(sizeof(struct addr) * src->page_server_num);
	memcpy(dst->page_server, src->page_server, sizeof(struct addr) * src->page_server_num);

	dst->req_header.attr = (struct http_attr *)malloc(sizeof(struct http_attr) * src->req_header.attr_num);
	for (int i = 0; i < src->req_header.attr_num; i++) {
		dst->req_header.attr[i].attr_name = strdup(src->req_header.attr[i].attr_name);
		dst->req_header.attr[i].attr_value = strdup(src->req_header.attr[i].attr_value);
	}
}

void destroy_conf(CONF *conf)
{
	if (conf) {
		if (conf->url_server)
			free(conf->url_server);
		if (conf->page_server);
			free(conf->page_server);
			if (conf->req_header.attr) {
				for (int i = 0; i < conf->req_header.attr_num; i++) {
					if (conf->req_header.attr[i].attr_name)
						free(conf->req_header.attr[i].attr_name);
					if (conf->req_header.attr[i].attr_value)
						free(conf->req_header.attr[i].attr_value);
				}
				free(conf->req_header.attr);
			}
	}
}

